#!/usr/bin/env python
import roslib; roslib.load_manifest('ua_objdb')

import rospy
from ua_objdb.msg import *
from ua_objdb.srv import *

import sys



def get_names_client():
    rospy.wait_for_service('get_names')
    try:
        get_names = rospy.ServiceProxy('get_names', GetNames)
        resp = get_names('')
        print 'registered objects:'
        for n in resp.names:
            print '---', n
    except rospy.ServiceException, e:
        print 'Service call failed: %s' % e



def save_object_client(name):
    rospy.wait_for_service('save_object')
    try:
        save_object = rospy.ServiceProxy('save_object', SaveObject)
        obj = Object(0,0,0)
        resp = save_object(name, obj)
        if resp.saved:
            print 'object saved'
    except rospy.ServiceException, e:
        print 'Service call failed: %s' % e



def fetch_object_client(name):
    rospy.wait_for_service('fetch_object')
    try:
        fetch_object = rospy.ServiceProxy('fetch_object', FetchObject)
        resp = fetch_object(name)
        print '%s(%s, %s, %s)' % (name, resp.obj.x, resp.obj.y, resp.obj.z)
    except rospy.ServiceException, e:
        print 'Service call failed: %s' % e



if __name__ == '__main__':
    if sys.argv[1] == 'names':
        get_names_client()
    elif sys.argv[1] == 'fetch':
        fetch_object_client(sys.argv[2])
    elif sys.argv[1] == 'save':
        save_object_client(sys.argv[2])

